package controllers;

import helpers.UrlHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import systems.ConfigSystem;

import admin.models.DomainModel;

import entities.Domain;
import entities.SiteConfig;

public class HomeServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		//Redirect old domain
		try {
			String path = ((HttpServletRequest) request).getRequestURI();
			String domain = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
			if (domain != null) {
				domain = domain.replace("/", "");
				if (!path.contains("/*/") && UrlHelper.checkDomain(domain)) {
					response.sendRedirect("/info/" + domain);
					return;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		TreeMap check = new TreeMap();
		String name = "";
		
		request.setAttribute("check", check);
		request.setAttribute("name", name);
		try {
			request.getRequestDispatcher("/pages/home.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		//Redirect old domain
		try {
			String path = ((HttpServletRequest) request).getRequestURI();
			String domain = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
			if (domain != null) {
				domain = domain.replace("/", "");
				if (!path.contains("/*/") && UrlHelper.checkDomain(domain)) {
					response.sendRedirect("/info/" + domain);
					return;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		TreeMap<String, String> check = new TreeMap<String, String>();
		String name = (String) request.getParameter("name");
		
		name = UrlHelper.fomatDomains(name);
		
		if (name == null || !UrlHelper.checkDomain(name)) {
			check.put("name", "domain is not valid!");
		}
		
		name = UrlHelper.getMainDomain(name);
		
		if (check.size() == 0) {
			/*DomainModel domainModel = DomainModel.getInstance();
			Domain domain = domainModel.get(name);
			if (domain == null) {
				response.sendRedirect("/process/" + name);
				return;
			} else {
				response.sendRedirect("/info/" + name);
				return;
			}*/
			
			response.sendRedirect("/info/" + name);
			return;
		}
		
		request.setAttribute("check", check);
		request.setAttribute("name", name);
		try {
			request.getRequestDispatcher("/pages/home.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
}
